package com.hrrt.home.admin.controller.manage;

import com.hrrt.common.base.BaseController;
import com.hrrt.home.common.constant.HomeResult;
import com.hrrt.home.common.constant.HomeResultConstant;
import com.hrrt.home.dao.model.*;
import com.hrrt.home.rpc.api.HomeBaseDeviceService;
import com.hrrt.home.rpc.api.HomeBrandService;
import com.hrrt.home.rpc.api.HomeDeviceTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 品牌controller
 * Created by 司建高 on 2017/09/13.
 */
@Controller
@RequestMapping("/manage/brand")
@Api(value = "品牌", description = "品牌")
public class HomeBrandController extends BaseController {

    @Autowired
    private HomeBrandService homeBrandService;

    @Autowired
    private HomeBaseDeviceService homeBaseDeviceService;

    @Autowired
    private HomeDeviceTypeService homeDeviceTypeService;

    private static Logger _log = LoggerFactory.getLogger(HomeBrandController.class);

    @ApiOperation(value = "品牌首页")
    @RequiresPermissions("home:brand:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/brand/index.jsp";
    }

    @ApiOperation(value = "品牌列表")
    @RequestMapping(value="list",method = RequestMethod.GET)
    @RequiresPermissions("home:brand:read")
    @ResponseBody
    public Object list (@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
                        @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
                        @RequestParam(required = false, value = "sort") String sort,
                        @RequestParam(required = false, value = "order") String order) {
        HomeBrandExample homeBrandExample = new HomeBrandExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            homeBrandExample.setOrderByClause(sort + " " + order);
        }

        homeBrandExample.createCriteria().andParentIdEqualToWithOtherName(0);
        List<HomeBrand> rows = homeBrandService.selectByExampleWithNameForOffsetPage(homeBrandExample, offset, limit);
        int total = homeBrandService.countByExample(homeBrandExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增品牌")
    @RequiresPermissions("home:brand:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(ModelMap modelMap) {

        HomeBaseDeviceExample homeBaseDeviceExample = new HomeBaseDeviceExample();
        homeBaseDeviceExample.createCriteria().andParentIdEqualTo(0);

        List<HomeBaseDevice> homeBaseDevices = homeBaseDeviceService.selectByExample(homeBaseDeviceExample);
        modelMap.put("homeBaseDevices", homeBaseDevices);

        return "/manage/brand/create.jsp";
    }

    @ApiOperation(value = "新增品牌")
    @RequiresPermissions("home:brand:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(HomeBrand homeBrand) {
        homeBrand.setCreateTime(new Date());
        homeBrand.setParentId(0);

        int count = homeBrandService.insertSelective(homeBrand);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

    @ApiOperation(value = "修改品牌")
    @RequiresPermissions("home:brand:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {

        HomeBaseDeviceExample homeBaseDeviceExample = new HomeBaseDeviceExample();
        homeBaseDeviceExample.createCriteria().andParentIdEqualTo(0);

        List<HomeBaseDevice> homeBaseDevices = homeBaseDeviceService.selectByExample(homeBaseDeviceExample);
        modelMap.put("homeBaseDevices", homeBaseDevices);

        HomeBrand homeBrand = homeBrandService.selectByPrimaryKey(id);
        modelMap.put("homeBrand",homeBrand);

        return "/manage/brand/update.jsp";
    }

    @ApiOperation(value = "修改品牌")
    @RequiresPermissions("home:brand:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") int id, HomeBrand homeBrand) {
        homeBrand.setId(id);
        int count = homeBrandService.updateByPrimaryKey(homeBrand);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

    @ApiOperation(value = "删除品牌")
    @RequiresPermissions("home:brand:delete")
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = homeBrandService.deleteByPrimaryKeys(ids);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

    @ApiOperation(value = "型号首页")
    @RequiresPermissions("home:brand:orderRead")
    @RequestMapping(value = "/orderIndex", method = RequestMethod.GET)
    public String orderIndex() {
        return "/manage/brand/order-index.jsp";
    }

    @ApiOperation(value = "型号列表")
    @RequestMapping(value="orderList",method = RequestMethod.GET)
    @RequiresPermissions("home:brand:orderRead")
    @ResponseBody
    public Object orderList (@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
                        @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
                        @RequestParam(required = false, value = "sort") String sort,
                        @RequestParam(required = false, value = "order") String order) {
        HomeBrandExample homeBrandExample = new HomeBrandExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            homeBrandExample.setOrderByClause(sort + " " + order);
        }

        homeBrandExample.createCriteria().andParentIdNotEqualToWithOtherName(0);

        List<HomeBrand> rows = homeBrandService.selectOrderByExampleWithNameForOffsetPage(homeBrandExample, offset, limit);
        int total = homeBrandService.countByExample(homeBrandExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增型号")
    @RequiresPermissions("home:brand:orderCreate")
    @RequestMapping(value = "/orderCreate", method = RequestMethod.GET)
    public String orderCreate(ModelMap modelMap) {
        HomeBrandExample homeBrandExample = new HomeBrandExample();
        homeBrandExample.createCriteria().andParentIdEqualTo(0);
        List<HomeBrand> homeBrands = homeBrandService.selectByExample(homeBrandExample);
        modelMap.put("homeBrands", homeBrands);

        return "/manage/brand/order-create.jsp";
    }

    @ApiOperation(value = "新增型号")
    @RequiresPermissions("home:brand:orderCreate")
    @RequestMapping(value = "/orderCreate", method = RequestMethod.POST)
    @ResponseBody
    public Object orderCreate(HomeBrand homeBrand) {
        HomeBrand parent = homeBrandService.selectByPrimaryKey(homeBrand.getParentId());
        homeBrand.setTypeId(parent.getTypeId());
        homeBrand.setCreateTime(new Date());

        int count = homeBrandService.insertSelective(homeBrand);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

    @ApiOperation(value = "修改型号")
    @RequiresPermissions("home:brand:orderUpdate")
    @RequestMapping(value = "/orderUpdate/{id}", method = RequestMethod.GET)
    public String orderUpdate(@PathVariable("id") int id, ModelMap modelMap) {

        HomeBrandExample homeBrandExample = new HomeBrandExample();
        homeBrandExample.createCriteria().andParentIdEqualTo(0);
        List<HomeBrand> homeBrands = homeBrandService.selectByExample(homeBrandExample);
        modelMap.put("homeBrands", homeBrands);

        HomeBrand homeBrand = homeBrandService.selectByPrimaryKey(id);
        modelMap.put("homeBrand",homeBrand);

        return "/manage/brand/order-update.jsp";
    }

    @ApiOperation(value = "修改型号")
    @RequiresPermissions("home:brand:orderUpdate")
    @RequestMapping(value = "/orderUpdate/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object orderUpdate(@PathVariable("id") int id, HomeBrand homeBrand) {
        homeBrand.setId(id);
        HomeBrand parent = homeBrandService.selectByPrimaryKey(homeBrand.getParentId());
        homeBrand.setTypeId(parent.getTypeId());
        int count = homeBrandService.updateByPrimaryKey(homeBrand);
        return new HomeResult(HomeResultConstant.SUCCESS,count);
    }

}
